import numpy as np
import time
import os
os.environ["OPENCV_VIDEOIO_MSMF_ENABLE_HW_TRANSFORMS"] = "0"
import cv2

#0,1是按插的顺序来的，R插在左边的USB2.0借口，L插在右边的USB3接口
Lcap = cv2.VideoCapture(1,cv2.CAP_MSMF)#打开内置摄像机
#Lcap = cv2.VideoCapture(cv2.CAP_DSHOW+1)#打开内置摄像机
Lcap.set(cv2.CAP_PROP_FRAME_HEIGHT,1080)
Lcap.set(cv2.CAP_PROP_FRAME_WIDTH,1920)
Lcap.set(cv2.CAP_PROP_FPS,30)
Rcap = cv2.VideoCapture(0,cv2.CAP_MSMF)#打开内置摄像机
#Rcap = cv2.VideoCapture(cv2.CAP_DSHOW+0)#打开内置摄像机
Rcap.set(cv2.CAP_PROP_FRAME_HEIGHT,1080)
Rcap.set(cv2.CAP_PROP_FRAME_WIDTH,1920)
Rcap.set(cv2.CAP_PROP_FPS,30)
flag = 1#播放视频
count = 0;#记录照相的次数
print(Lcap.isOpened(),Rcap.isOpened())
while Lcap.isOpened() and Rcap.isOpened():#当摄像头打开时
    ti=time.time()
    ret,L=Lcap.read()#读取当前摄像头画面
    ti2=time.time()
    ret,R=Rcap.read()
    print(ti2-ti,time.time()-ti2)
    cv2.imshow('l',L)#显示当前摄像头画面q
    cv2.imshow('r',R)
    if cv2.waitKey(flag)==ord(' '):#按下空格键拍照
        c = str(count)#将countint转换成str型
        cv2.imwrite('L'+c+'.jpg',L)#保存图片
        cv2.imwrite('R'+c+'.jpg',R)#保存图片
        count=count+1#计数加一
    if cv2.waitKey(flag)==ord('q'):#退出循环
        break;
cv2.destroyAllWindows()#关闭所有窗口
Lcap.release()#释放摄像头
Rcap.release()